export const numberDecimal2 = {
  pattern: /^[1-9]{1}\d*(\.\d{0,2})?$/,
  message: '请输入最多两位小数的数字',
};
export const integerRule = {
  pattern: /^[1-9]{1}\d*$/,
  message: '请输入正整数',
};
export const requireRule = {
  message: '必须填写该项',
  require: true,
};
export function validateItem(value: string, key: string, patternMap?: any, flag?: boolean) {
  const rule = patternMap[key];
  if (!rule) {
    return true;
  }
  if (rule.pattern?.test(value)) {
    return true;
  } else if ((!rule.require && !value) || (rule.require && value)) {
    return true;
  }
  if (!flag) {
    console.log('miku', value, key, patternMap, flag);
    ElMessage.warning(rule.message);
  }
  return false;
}
export function validate(tableData: any[], patternMap: any) {
  for (let i = 0; i < tableData.length; i++) {
    const row: any = tableData[i];
    for (const key in row) {
      const value = row[key];
      if (!validateItem(value, key, patternMap, true)) {
        console.log(value, key, patternMap);
        return false;
      }
    }
  }
  return true;
}
